package com.snail.snailvr.views;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.widget.TextView;

import com.snail.snailvr.R;
import com.snail.snailvr.utils.AnimationUtil;
import com.snail.snailvr.utils.LockUtil;
import com.snail.snailvr.widget.CustomLockView;

import java.util.Timer;
import java.util.TimerTask;

/**
 * 登陆验证页面
 * Created by shiq on 6/20/16.
 */
public class GestureUnlockActivity extends GestureBaseActivity {
    private TextView tvWarn;

    Context context;

    private static final String REMOVE_PSW_KEY = "remove_psw_key";
    private boolean isRemovePsw;

    public static Intent getIntent(Activity activity, boolean removePsw){
        Intent intent = new Intent(activity, GestureUnlockActivity.class);
        intent.putExtra(REMOVE_PSW_KEY, removePsw);
        return intent;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = this;
    }

    @Override
    protected void handleIntent() {
        isRemovePsw = getIntent().getBooleanExtra(REMOVE_PSW_KEY, false);
    }

    /**
     * 初始化控件
     */
    @Override
    protected void initView() {
        tvWarn = getViewById(R.id.tvWarn);

        CustomLockView cl = (CustomLockView)findViewById(R.id.cl);
        cl.setmIndexs(LockUtil.getPwd(this));
        cl.setErrorTimes(4);
        cl.setStatus(1);
        cl.setShow(true);
        cl.setOnCompleteListener(new CustomLockView.OnCompleteListener() {
            @Override
            public void onComplete(int[] indexs) {
                tvWarn.setText(getResources().getString(R.string.Success));
                tvWarn.setTextColor(getResources().getColor(R.color.gesture_blue));

                if(isRemovePsw) {
                    LockUtil.clearPsw(context);
                }

                TimerTask task = new TimerTask() {
                    @Override
                    public void run() {
                        finish();
                    }
                };
                Timer timer = new Timer();
                timer.schedule(task, 1500);

            }
            @Override
            public void onError(String msg) {
                tvWarn.setText(msg);
                tvWarn.setTextColor(getResources().getColor(R.color.gesture_red));
                tvWarn.startAnimation(AnimationUtil.getSharkAnimation());
            }
        });
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_gesture_unlock;
    }

    @Override
    public void onBackPressed() {
        if (isRemovePsw){
            finish();
        }
    }

    @Override
    protected boolean onToolbarMenuItemClick(MenuItem item) {
        return false;
    }
}
